package apple.ui.swt;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import apple.core.Apple;

public class AppleII {

   static Logger logger = Logger.getLogger(AppleII.class);

   /**
    * @param args
    */
   public static void main(String[] args) throws Exception {
      BasicConfigurator.configure();

      Display display = new Display();
      Shell shell = new Shell(display);
      shell.setText(Apple.APPLICATION_TITLE);
      shell.setLayout(new RowLayout());
      final Monitor monitor = new Monitor(shell);
      final Keyboard keyboard = new Keyboard();
      monitor.addKeyListener(keyboard);
      final Apple apple = new Apple("Christiaan's Apple IIe");
      keyboard.setKeyboardDecoder(apple.getKeyboardDecoder());
      apple.setMonitor(monitor);
      shell.addShellListener(new ShellAdapter() {
         public void shellClosed(ShellEvent e) {
            apple.shutdown();
         }

         public void shellIconified(ShellEvent e) {
            apple.suspend();
         }

         public void shellDeiconified(ShellEvent e) {
            apple.resume();
         }
      });
      shell.open();

      apple.startup();

      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) {
            display.sleep();
         }
      }
      display.dispose();

      logger.debug(String.format("Thread \"%s\" finished", Thread
            .currentThread().getName()));
   }
}
